﻿using RFIDLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace a_itc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //a_itc();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] portbug = Port_Seek(); 
            comboBox2.Items.Clear();
            for (int i = 0; i < portbug.Length; i++)
            {
                comboBox2.Items.Add(portbug[i].ToUpper());
            }
            if (comboBox2.SelectedIndex >-1)
            {
                comboBox2.SelectedIndex = 0;
            }
            

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            rFID_Class.RFID_Dispose(out string mes);
        }

        RFID_Class rFID_Class = new RFID_Class();

        public void a_itc()
        {
            InitializeComponent();

            //string Station = "P10";
            //var parrern = @"^(O|P)(10|[1-9])$";
            //var OK = Regex.IsMatch(Station, parrern);
            ;


            string mes = "";
            string RFID_Class = string.Empty;
            string[] RFID_value = new string[2] { "D01A240508000002C1P2P3P4P5P6P7P8", "D01A240508000002C1P2P3P4P5P6P7P8" };
            string[] RFID_value2 = new string[2] { "", "" };
            bool[] RFID_state = new bool[2];
            RFID_value2[0] = RFID_value[0].Replace("\r", "").Replace("\n", "");

            int Rut = rFID_Class.RFID_init("COM3", 115200, out mes);
            if (Rut == 0)
            {
                ;
                //var uuuuu = rFID_Class.RFID_Check(RFID_value,out RFID_state[0],out mes);

                //var uuuuu = rFID_Class.RFID_Check("P10","D",out RFID_state,out RFID_value, out mes);

                var hhhhhh = rFID_Class.RFIDState_Write("P10", new string[2] { "OK", "" }, out RFID_value, out mes);
                //var hhhhhh2 = rFID_Class.RFIDState_Write("P9", new string[2] { "NG", "OK" }, RFID_value,out RFID_value, out mes);

                // MessageBox.Show(mes);
                ;
            }

            
        }
        public string[] Port_Seek()
        {
            string[] portNames = SerialPort.GetPortNames();
            return portNames;
        }

        //预写入log信息
        object thisObj = new object();
        public void PrintLog(String strmessage)
        {
            lock (thisObj)
            {
                Task.Run(() =>
                {
                    //写入List控件
                    if (listBox1 != null)
                    {
                        if (listBox1.InvokeRequired == true)
                        {
                            listBox1.Invoke(new Action(() => {
                                if (listBox1.Items.Count > 100) { listBox1.Items.Clear(); }
                                listBox1.Items.Add(strmessage);
                                //光标跟随
                                listBox1.SelectedIndex = listBox1.Items.Count - 1;
                                listBox1.SelectedItems.Clear();
                            }));
                        }
                        else
                        {
                            if (listBox1.Items.Count > 100) { listBox1.Items.Clear(); }
                            listBox1.Items.Add(strmessage);
                            //光标跟随
                            listBox1.SelectedIndex = listBox1.Items.Count - 1;
                            listBox1.SelectedItems.Clear();
                        }
                    }
                });
            }
        }
        private void button15_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string port_str = comboBox2.Text;
            string Band_str = comboBox1.Text;
            int band = 0;

            if (!port_str.Contains("COM"))
            {
                MessageBox.Show("错误无端口","提示");
                return;
            }
            if (Band_str == string.Empty)
            {
                MessageBox.Show("错误无端口","提示");
                return;
            }
            else
            {
                int.TryParse(Band_str,out band);
            }

            int Rut = rFID_Class.RFID_init(port_str, band, out string mes);
            if (Rut == 0)
            {
                //MessageBox.Show("端口打开成功！","提示");
                PrintLog("端口打开成功!");
                RFID_Class.RFIDLogInfo("端口打开成功!");
            }
            else
            {
                PrintLog("端口打开失败!");
                RFID_Class.RFIDLogInfo("端口打开失败!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (rFID_Class.RFID_Dispose(out string mes)!=0)
            {
                PrintLog("端口关闭失败!");
                RFID_Class.RFIDLogInfo("端口关闭失败!");
            }
        }

        //读取
        private void button3_Click(object sender, EventArgs e)
        {
            if (!checkBox1.Checked && !checkBox2.Checked)
            {
                PrintLog("未选择要读取的工位：数据读取失败！");
                RFID_Class.RFIDLogInfo("未选择要读取的工位：数据读取失败！");
                return;
            }

            if (rFID_Class.RFID_Read(0x24, out string[] data, out string mes) == 0)
            {
                PrintLog("数据读取成功：" + mes);
                RFID_Class.RFIDLogInfo("数据读取成功：" + mes);
            }
            else
            {
                PrintLog("数据读取失败：" + mes);
                RFID_Class.RFIDLogInfo("数据读取失败：" + mes);
                return;
            }

            if (checkBox1.Checked)
            {
                PrintLog("A工位：" + data[0]);
                RFID_Class.RFIDLogInfo("A工位：" + data[0]);
            }
            if (checkBox2.Checked)
            {
                PrintLog("B工位：" + data[1]);
                RFID_Class.RFIDLogInfo("B工位：" + data[1]);
            }
        }

        //写入
        private void button4_Click(object sender, EventArgs e)
        {
            string strA = textBox3.Text.Trim().Replace(" ","");
            string strB = textBox4.Text.Trim().Replace(" ", "");

            if (strA.Length < 37)
            {
                strA=strA.PadRight(37, ' ');
            }
            if (strB.Length < 37)
            {
                strB = strB.PadRight(37, ' ');
            }

            if (checkBox1.Checked)
            {
                if (strA == string.Empty)
                {
                    PrintLog("A工位：数据为空写入失败！");
                    RFID_Class.RFIDLogInfo("A工位：数据为空写入失败！");
                    return;
                }
                if (rFID_Class.RFID_Write(rFID_Class.address_A, strA, out string mes) == 0)
                {
                    ;
                }
                else
                {
                    PrintLog("A工位：数据写入失败！" + strA + ","+ mes);
                    RFID_Class.RFIDLogInfo("A工位：数据写入失败！" + strB + "," + mes);
                }
            }
            if (checkBox2.Checked)
            {
                if (strB == string.Empty)
                {
                    PrintLog("B工位：数据为空写入失败！");
                    RFID_Class.RFIDLogInfo("B工位：数据为空写入失败！");
                    return;
                }
                if (rFID_Class.RFID_Write(rFID_Class.address_B, strB, out string mes) == 0)
                {
                    ;
                }
                else
                {
                    PrintLog("B工位：数据写入失败！" + strB + "," + mes);
                    RFID_Class.RFIDLogInfo("B工位：数据写入失败！" + strB + "," + mes);
                }
            }

            if (!checkBox1.Checked && !checkBox2.Checked)
            {
                PrintLog("未选择要写入的工位：数据写入失败！");
                RFID_Class.RFIDLogInfo("未选择要写入的工位：数据写入失败！");
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            string strA = textBox1.Text.Trim().Replace(" ", "");
            string strB = textBox2.Text.Trim().Replace(" ", "");
            if (strA == string.Empty ||strB == string.Empty)
            {
                PrintLog("写入必要参数为空：数据写入失败！");
                RFID_Class.RFIDLogInfo("写入必要参数为空：数据写入失败！");
            }
            if (!checkBox3.Checked && !checkBox4.Checked)
            {
                PrintLog("未选择要写入工位的状态：数据写入失败！");
                RFID_Class.RFIDLogInfo("未选择要写入工位的状态：数据写入失败！");
                return;
            }

           string date_str1 = DateTime.Now.ToString("yyMMdd") + "000001";
           string date_str2 = DateTime.Now.ToString("yyMMdd") + "000002";
            string str1 = "";

           Button button =  sender as Button;
            if (button != null )
            {
                switch (button.Text)
                {
                    case "P1":
                        if (checkBox3.Checked) { str1 = "P1"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + str1, strA + strB + "B" + date_str2 + str1 });
                        break;
                    case "P2":
                        if (checkBox3.Checked) { str1 = "P2"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1"+ str1, strA + strB + "B" + date_str2 + "P1"+ str1 });
                        break;
                    case "P3":
                        if (checkBox3.Checked) { str1 = "P3"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2" + str1, strA + strB + "B" + date_str2 + "P1P2" + str1 });
                        break;
                    case "P4":
                        if (checkBox3.Checked) { str1 = "P4"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3" + str1, strA + strB + "B" + date_str2 + "P1P2P3" + str1 });
                        break;
                    case "P5":
                        if (checkBox3.Checked) { str1 = "P5"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3P4" + str1, strA + strB + "B" + date_str2 + "P1P2P3P4" + str1 });
                        break;
                    case "P6":
                        if (checkBox3.Checked) { str1 = "P6"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3P4P5" + str1, strA + strB + "B" + date_str2 + "P1P2P3P4P5" + str1 });
                        break;
                    case "P7":
                        if (checkBox3.Checked) { str1 = "P7"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3P4P5P6" + str1, strA + strB + "B" + date_str2 + "P1P2P3P4P5P6" + str1 });
                        break;
                    case "P8":
                        if (checkBox3.Checked) { str1 = "P8"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3P4P5P6P7" + str1, strA + strB + "B" + date_str2 + "P1P2P3P4P5P6P7" + str1 });
                        break;
                    case "P9":
                        if (checkBox3.Checked) { str1 = "P9"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3P4P5P6P7P8" + str1, strA + strB + "B" + date_str2 + "P1P2P3P4P5P6P7P8" + str1 });
                        break;
                    case "P10":
                        if (checkBox3.Checked) { str1 = "P10"; }
                        if (checkBox4.Checked) { str1 = "NG"; }
                        Wirte_ID(new string[] { strA + strB + "A" + date_str1 + "P1P2P3P4P5P6P7P8P9" + str1, strA + strB + "B" + date_str2 + "P1P2P3P4P5P6P7P8P9" + str1 });
                        break;
                }

            }

        }


        public void Wirte_ID(string[] ID_Str)
        {
            if (ID_Str[0].Length < 37)
            {
                ID_Str[0] = ID_Str[0].PadRight(37, ' ');
            }
            if (ID_Str[1].Length < 37)
            {
                ID_Str[1] = ID_Str[1].PadRight(37, ' ');
            }

            if (rFID_Class.RFID_Write(rFID_Class.address_A, ID_Str[0], out string mes) == 0)
            {
                PrintLog("A工位：数据写入成功！" + ID_Str[0] + "," + mes);
                RFID_Class.RFIDLogInfo("A工位：数据写入成功！" + ID_Str[0] + "," + mes);
            }
            else
            {
                PrintLog("A工位：数据写入失败！" + ID_Str[0] + "," + mes);
                RFID_Class.RFIDLogInfo("A工位：数据写入失败！" + ID_Str[0] + "," + mes);
            }

            if (rFID_Class.RFID_Write(rFID_Class.address_B, ID_Str[1], out string mes1) == 0)
            {
                PrintLog("B工位：数据写入成功！" + ID_Str[0] + "," + mes);
                RFID_Class.RFIDLogInfo("B工位：数据写入成功！" + ID_Str[1] + "," + mes);
            }
            else
            {
                PrintLog("B工位：数据写入失败！" + ID_Str[1] + "," + mes);
                RFID_Class.RFIDLogInfo("B工位：数据写入失败！" + ID_Str[1] + "," + mes1);
            }

        }


        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                e.Handled = true; // 阻止空格的输入
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                e.Handled = true; // 阻止空格的输入
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                checkBox4.Checked = false;
            }

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox3.Checked = false;
            }
        }
    }
}
